home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 35
/
Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso
/
Aminet
/
game
/
shoot
/
ADescentSrc.lha
/
descent
/
main
/
hash.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-04-24
|
4KB
|
172 lines
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
* $Source: /usr/CVS/descent/main/hash.c,v $
* $Revision: 1.3 $
* $Author: tfrieden $
* $Date: 1998/04/24 14:27:51 $
*
* Functions to do hash table lookup.
*
* $Log: hash.c,v $
* Revision 1.3 1998/04/24 14:27:51 tfrieden
* Debug
*
* Revision 1.2 1998/03/22 16:07:04 hfrieden
* What the hell did I do here? CVS says Locally Modified?
*
* Revision 1.1.1.1 1998/03/03 15:12:23 nobody
* reimport after crash from backup
*
* Revision 1.1.1.1 1998/02/13 20:20:55 hfrieden
* Initial Import
*
* Revision 2.0 1995/02/27 11:28:01 john
* New version 2.0, which has no anonymous unions, builds with
* Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
*
* Revision 1.5 1994/12/05 23:37:06 matt
* Took out calls to warning() function
*
* Revision 1.4 1994/05/09 20:02:33 john
* Fixed bug w/ upper/lower case.
*
* Revision 1.3 1994/05/06 15:31:51 john
* Don't add duplicate names to the hash table.
*
* Revision 1.2 1994/05/03 16:45:35 john
* Added hash table lookup to speed up loading.
*
* Revision 1.1 1994/05/03 10:36:41 john
* Initial revision
*
*
*/
#pragma off (unreferenced)
static char rcsid[] = "$Id: hash.c,v 1.3 1998/04/24 14:27:51 tfrieden Exp $";
#pragma on (unreferenced)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "mono.h"
#include "hash.h"
#include "key.h"
extern int Inferno_verbose;
int hashtable_init( hashtable *ht, int size ) {
int i;
ht->size=0;
for (i=1; i<16; i++ ) { // CHANGED: Was 13
if ( (1<<i) >= size ) {
ht->bitsize = i;
ht->size = 1<<i;
break;
}
}
size = ht->size;
ht->and_mask = ht->size - 1;
if (ht->size==0)
Error( "Hashtable has size of 0" );
ht->key = (char **)calloc(1, size * sizeof(char *) ); // CHANGED: was malloc
if (ht->key==NULL)
Error( "Not enough memory to create a hash table of size %d", size );
for (i=0; i<size; i++ )
ht->key[i] = NULL;
// Use calloc cause we want zero'd array.
ht->value = calloc(1, size*sizeof(int) ); // CHANGED: was malloc
if (ht->value==NULL) {
free(ht->key);
Error( "Not enough memory to create a hash table of size %d\n", size );
}
ht->nitems = 0;
if (Inferno_verbose) printf("Creating hashtable, size %d(%d)\n", size, ht->bitsize);
return 0;
}
void hashtable_free( hashtable *ht ) {
if (ht->key != NULL )
free( ht->key );
if (ht->value != NULL )
free( ht->value );
ht->size = 0;
}
int hashtable_getkey( char *key ) {
int k = 0, i=0;
while( *key ) {
k^=((int)(*key++))<<i;
i++;
}
return k;
}
int hashtable_search( hashtable *ht, char *key ) {
int i,j,k;
strlwr( key );
k = hashtable_getkey( key );
i = 0;
while(i < ht->size ) {
j = (k+(i++)) & ht->and_mask;
if ( ht->key[j] == NULL )
return -1;
if (!stricmp(ht->key[j], key ))
return ht->value[j];
}
return -1;
}
void hashtable_insert( hashtable *ht, char *key, int value ) {
int i,j,k;
// mprintf( 0, "Inserting '%s' into hash table\n", key );
// key_getch();
strlwr( key );
k = hashtable_getkey( key );
i = 0;
while(i < ht->size) {
j = (k+(i++)) & ht->and_mask;
// Assert( j < ht->size );
// mprintf( 0, "Word '%s' (%d) at level %d has value of %d\n", key, k, i-1, j );
// mprintf( 0, "ht->key[%d]=%.8x\n", j, ht->key[j] );
if ( ht->key[j] == NULL ) {
ht->nitems++;
ht->key[j] = key;
ht->value[j] = value;
return;
} else if (!stricmp( key, ht->key[j] )) {
return;
}
}
Error( "Out of hash slots (%d)\n", ht->size );
}